package com.study.manage.mvc.v2;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.EvaluateRepository;
import com.study.manage.domain.Evaluate;
import com.study.manage.dto.AjaxResponse;
import com.study.manage.mvc.common.UploadController;


@Controller
@RequestMapping("/v2/task")
public class V2TaskController extends UploadController {

    @Autowired
    private EvaluateRepository evaluateRepository;
    @Autowired
    private V2EvaluateController v2EvaluateController;

    @ResponseBody
    @RequestMapping(path = "evaluate", method = RequestMethod.GET)
    public AjaxResponse evaluate(HttpServletRequest request) {
        AjaxResponse ajax = new AjaxResponse(true);
        Calendar nowcal = Calendar.getInstance();
        nowcal.add(Calendar.DAY_OF_MONTH, -1);
        List<Evaluate> es = evaluateRepository.findByBeginAtAndEndAt(nowcal.getTime());
        for (Evaluate e : es) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(e.beginAt);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            while (cal.getTime().getTime() <= e.endAt.getTime()) {
                v2EvaluateController.list_applyExportDay(cal.getTime(), e.id, true);
                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        return ajax;
    }

}
